package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.Details0Bean2;
import com.wnhz.work.yz.utils.GlideImageLoader;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.youth.banner.Banner;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InstallDetails33Activity extends BaseActivity implements View.OnClickListener {
    private Banner banner;
    private TextView title, info, price, area, comnum, danwei;
    private RecyclerView commit;
    private EditText remark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_details33);
        initTitle();
        initView();
        upLoad();
    }

    private void initView() {
        remark = (EditText) findViewById(R.id.remark);
        commit = (RecyclerView) findViewById(R.id.commit);
        commit.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        findViewById(R.id.look).setOnClickListener(this);
        findViewById(R.id.look2).setOnClickListener(this);
        findViewById(R.id.btn).setOnClickListener(this);
        banner = (Banner) findViewById(R.id.banner);
        title = (TextView) findViewById(R.id.title);
        info = (TextView) findViewById(R.id.info);
        area = (TextView) findViewById(R.id.your_area);
        comnum = (TextView) findViewById(R.id.comnum);
        danwei = (TextView) findViewById(R.id.danwei);
        price = (TextView) findViewById(R.id.price);
    }

    private Details0Bean2 bean = new Details0Bean2();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("pid", pid);
        map.put("type", getIntent().getStringExtra("type"));
        showDialog();
        XUtil.Post(Url.DECORATE_DETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        bean = gson.fromJson(result, Details0Bean2.class);
                        Details0Bean2.InfoEntity info = bean.getInfo();
                        setData(info);
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });
    }

    private String pid = "";
    private String name = "";

    private void initTitle() {
        pid = getIntent().getStringExtra("pid");
        name = getIntent().getStringExtra("name");
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("" + name);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        ImageView ic_telephone = (ImageView) findViewById(R.id.ic_telephone);
        ic_telephone.setOnClickListener(this);
        ic_telephone.setVisibility(View.GONE);
    }
private TextView pingjia;
    private void setData(final Details0Bean2.InfoEntity info) {
        pingjia= (TextView) findViewById(R.id.pingjia);
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(info.getIndex_pic());
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        //
        title.setText("" + info.getName());
        this.info.setText("" + info.getDes());
        danwei.setText("" + info.getUnit());
        price.setText("¥" + info.getPrice() + "/" + info.getUnit());
        pingjia.setText("用户评价：(" + info.getComment_nums() + ")");
        comnum.setText("" + info.getComment_nums());
        commit.setAdapter(new BaseRVAdapter(this, info.getComment()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_comment2;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageLoader.getInstance().displayImage(info.getComment().get(position).getHead_img(), holder.getImageView(R.id.head));
                holder.setTextView(R.id.name, info.getComment().get(position).getRealname());
                holder.setTextView(R.id.time, info.getComment().get(position).getAdd_time());
                holder.setTextView(R.id.info, info.getComment().get(position).getContent());


                final List<String> img = info.getComment().get(position).getImg();
                LinearLayout status1 = holder.getView(R.id.status1);
                LinearLayout status2 = holder.getView(R.id.status2);
                LinearLayout status3 = holder.getView(R.id.status3);
                ImageView img1 = holder.getImageView(R.id.img1);
                ImageView img2_1 = holder.getImageView(R.id.img2_1);
                ImageView img2_2 = holder.getImageView(R.id.img2_2);
                RecyclerView img_recycler = holder.getView(R.id.img_recycler);
                switch (img.size()) {
                    case 0:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);
                        break;
                    case 1:
                        status1.setVisibility(View.VISIBLE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.GONE);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                img1);
                        img1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(InstallDetails33Activity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        break;
                    case 2:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.VISIBLE);
                        status3.setVisibility(View.GONE);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
                                img2_1);
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(1),
                                img2_2);
                        img2_1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(InstallDetails33Activity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 0);
                                startActivity(intent);
                            }
                        });
                        img2_2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(InstallDetails33Activity.this, ShowImageDetail.class);
                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                intent.putExtra("index", 1);
                                startActivity(intent);
                            }
                        });
                        break;
                    default:
                        status1.setVisibility(View.GONE);
                        status2.setVisibility(View.GONE);
                        status3.setVisibility(View.VISIBLE);
                        img_recycler.setLayoutManager(new GridLayoutManager(InstallDetails33Activity.this, 3, LinearLayoutManager.VERTICAL, false));
                        img_recycler.setAdapter(new BaseRVAdapter(InstallDetails33Activity.this, img) {
                            @Override
                            public int getLayoutId(int viewType) {
                                return R.layout.item_photo;
                            }

                            @Override
                            public void onBind(BaseViewHolder holder, final int position) {
                                holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(InstallDetails33Activity.this, ShowImageDetail.class);
                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
                                        intent.putExtra("index", position);
                                        startActivity(intent);
                                    }
                                });
                                ImageLoader.getInstance().displayImage(img.get(position), holder.getImageView(R.id.img));
                            }

                        });

                        break;
                }

            }

        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.look:
            case R.id.look2:
                startActivity(new Intent(this, LookComActivity.class).putExtra("pid", pid).putExtra("type",  getIntent().getStringExtra("type")));

                break;
            case R.id.btn:
                if (MyApplication.getInstance().getUserBean() == null) {
                    MyToast("请登录");
                    startActivity(new Intent(getApplicationContext(), LoginFirstActivity.class));
                    return;
                }
                Intent intent = new Intent(this, InstallYuYue3Activity.class);
                intent.putExtra("type", getIntent().getStringExtra("type"));
                intent.putExtra("pid", pid);
                intent.putExtra("price", bean.getInfo().getPrice());
                intent.putExtra("danwei", bean.getInfo().getUnit());
                if ("".equals(area.getText().toString())) {
                    intent.putExtra("num", 0);
                } else {
                    try {
                        intent.putExtra("num",Integer.parseInt( area.getText().toString()));
                    } catch (Exception e) {
                        intent.putExtra("num", 0);

                    }
                }
                intent.putExtra("remark", remark.getText().toString());
                startActivity(intent);


                break;
        }
    }
}
